package com.rankeiot.core.excel;

import cn.hutool.core.io.IORuntimeException;

import java.io.IOException;
import java.io.Writer;
import java.util.Collection;

/**
 * 简单的Html写法
 */
public class ExcelStreamWriter {
    private Writer writer;
    private static String before = "<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n" +
            "      xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">\n" +
            "<head>\n" +
            "    <meta charset=\"utf-8\">\n" +
            "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n" +
            "    <meta name=\"ProgId\" content=\"Excel.Sheet\">\n" +
            "    <meta name=\"Generator\" content=\"WPS Office ET\">\n" +
            "   " +
            "    <style>\n" +
            "        table {\n" +
            "            border: 0;\n" +
            "            margin: 0;\n" +
            "            border-spacing: 0;\n" +
            "            border-collapse: collapse;\n" +
            "            table-layout: fixed;\n" +
            "        }\n" +
            "        table td {\n" +
            "            padding: 0;\n" +
            "            font-family: \"宋体\";\n" +
            "            font-size: 11.0pt;\n" +
            "            font-weight: 400;\n" +
            "            font-style: normal;\n" +
            "            mso-generic-font-family: auto;\n" +
            "            border: 0.5pt solid #7F7F7F;\n" +
            "            height: 12.00pt;\n" +
            "            width: 51.15pt;\n" +
            "        }\n" +
            "    </style>\n" +
            "    <!--[if gte mso 9]>\n" +
            "    <xml>\n" +
            "        <x:ExcelWorkbook>\n" +
            "            <x:ExcelWorksheets>\n" +
            "                <x:ExcelWorksheet>\n" +
            "                    <x:Name>Sheet1</x:Name>\n" +
            "                    <x:WorksheetOptions>\n" +
            "                        <x:DefaultRowHeight>240</x:DefaultRowHeight>\n" +
            "                        <x:StandardWidth>2338</x:StandardWidth>\n" +
            "                        <x:Selected/>\n" +
            "                        <x:Panes>\n" +
            "                            <x:Pane>\n" +
            "                                <x:Number>3</x:Number>\n" +
            "                                <x:ActiveCol>3</x:ActiveCol>\n" +
            "                                <x:ActiveRow>11</x:ActiveRow>\n" +
            "                                <x:RangeSelection>D12</x:RangeSelection>\n" +
            "                            </x:Pane>\n" +
            "                        </x:Panes>\n" +
            "                        <x:ProtectContents>False</x:ProtectContents>\n" +
            "                        <x:ProtectObjects>False</x:ProtectObjects>\n" +
            "                        <x:ProtectScenarios>False</x:ProtectScenarios>\n" +
            "                        <x:PageBreakZoom>100</x:PageBreakZoom>\n" +
            "                        <x:Print>\n" +
            "                            <x:PaperSizeIndex>9</x:PaperSizeIndex>\n" +
            "                        </x:Print>\n" +
            "                    </x:WorksheetOptions>\n" +
            "                </x:ExcelWorksheet>\n" +
            "            </x:ExcelWorksheets>\n" +
            "            <x:ProtectStructure>False</x:ProtectStructure>\n" +
            "            <x:ProtectWindows>False</x:ProtectWindows>\n" +
            "            <x:SelectedSheets>0</x:SelectedSheets>\n" +
            "            <x:WindowHeight>14760</x:WindowHeight>\n" +
            "            <x:WindowWidth>28800</x:WindowWidth>\n" +
            "        </x:ExcelWorkbook>\n" +
            "    </xml>\n" +
            "    <![endif]-->\n" +
            "</head>\n" +
            "<body link=\"blue\" vlink=\"purple\">\n" +
            "<table>\n" +
            "    ";
    private static final String end = "</table></body></html>";

    public ExcelStreamWriter(Writer writer) {
        this.writer = writer;
        initWrite();
    }

    private void initWrite() {
        try {
            writer.write(before);
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }
    }
    public void writeLine(Object ...data) throws IORuntimeException {
        try {
            writer.write("<tr>");
            for (Object obj : data) {
                if (obj == null) {
                    writer.write("<td></td>");
                } else {
                    writer.write("<td>");
                    writer.write(obj.toString());
                    writer.write("</td>");
                }
            }
            writer.write("</tr>");
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }
    }
    public void writeLine(Collection data) throws IORuntimeException {
        try {
            writer.write("<tr>");
            for (Object obj : data) {
                if (obj == null) {
                    writer.write("<td></td>");
                } else {
                    writer.write("<td>");
                    writer.write(obj.toString());
                    writer.write("</td>");
                }
            }
            writer.write("</tr>");
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }
    }
    public void finish(){
        try {
            writer.write(end);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }
    }
}
